package com.personal.gateway.web;

import com.alibaba.fastjson.JSONObject;
import com.personal.gateway.dto.ContainerGroup;
import com.personal.gateway.dto.UserLoginDto;
import com.personal.gateway.service.UserService;
import com.personal.gateway.service.WebIndexService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
public class WebIndexController {

    @Resource
    private WebIndexService webIndexService;

    @Resource
    private UserService userService;

    @GetMapping("/")
    public String index(ServerHttpRequest request, @CookieValue(value = "current-flink-name", defaultValue = "null") String name) {
        if (StringUtils.isBlank(name) || name.equals("null")) {
            return "index.html";
        }
        return "redirect:/" + name;
    }

    @GetMapping("/login")
    public String login() {
        return "login";
    }

    @ResponseBody
    @PostMapping("/login")
    public String login(@RequestBody UserLoginDto userLoginDto) {
        return userService.userLogin(userLoginDto);
    }


    @GetMapping("/flink-console.html")
    public String flinkConsole(ServerHttpRequest request) {
        return "flink-console";
    }

    @GetMapping("/flink-project-edit.html")
    public String flinkProjectEdit(ServerHttpRequest request) {
        return "flink-project-edit";
    }

    @GetMapping("/flink-operate.html")
    public String flinkOperate(ServerHttpRequest request) {
        return "flink-operate";
    }

    @GetMapping("/flink-list-job.html")
    public String flinkListJob(ServerHttpRequest request) {
        return "flink-list-job";
    }

    @GetMapping("/flink-list-task.html")
    public String flinkListTask(ServerHttpRequest request) {
        return "flink-list-task";
    }

    @GetMapping("/flink-project-history.html")
    public String flinkProjectHistory(ServerHttpRequest request) {
        return "flink-project-history";
    }

    @GetMapping("/flink-savepoint-history.html")
    public String flinkSavepointHistory(ServerHttpRequest request) {
        return "flink-savepoint-history";
    }

    @GetMapping("/flink-monitor-restart.html")
    public String flinkMonitorRestart(Model model, ServerHttpRequest request) {
        return "flink-monitor-restart";
    }

    @GetMapping("/flink-service-list.html")
    public String flinkServiceList(Model model, ServerHttpRequest request) {
        return "flink-service-list";
    }

    @ResponseBody
    @PostMapping("/get-flink-version")
    public Map<String, String> getFlinkVersion(ServerHttpRequest request) {
        return webIndexService.getFlinkVersion();
    }

    @ResponseBody
    @PostMapping("/flink-service-list-data")
    public Map<String, String> flinkServiceListData(ServerHttpRequest request, @RequestBody JSONObject obj) {
        return webIndexService.index(obj);
    }

    @ResponseBody
    @GetMapping("/jobManagerInfoList")
    public List<ContainerGroup> jobManagerInfoList(ServerHttpRequest request, @CookieValue(value = "exec-shell-name") String name) {
        return webIndexService.jobManagerInfoList(name);
    }

    @ResponseBody
    @GetMapping("/taskManagerInfoList")
    public List<ContainerGroup> taskManagerInfoList(ServerHttpRequest request, @CookieValue(value = "exec-shell-name") String name) {
        return webIndexService.taskManagerInfoList(name);
    }

    /**
     * flink 返回首页
     */
    @GetMapping("/overview")
    public String overview(Model model, ServerHttpRequest request) {
        return "index.html";
    }

}
